#include "driver.h"
#include "mfrc522.h"
#include "delay.h"
#include "stm32f10x.h"
//#include "main.h"
#include "myIIC.h"
#include "OLED_I2C.h"
#include "stm32f10x_tim.h"

/*
BEEP 	A0
LED		A1

FIRE	A14
READ	A15
TEMP	A4

WAVE	B13/B12
OLED	A6/A7
GSM		A9/A10
*/

//HC_SR04_Init
void HC_SR04_Init()
{  
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;  
    GPIO_InitTypeDef GPIO_InitStructure;
    RCC_APB2PeriphClockCmd(HC_SR04_CLK, ENABLE);
     
    GPIO_InitStructure.GPIO_Pin =HC_SR04_TR_PIN;  
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_Init(HC_SR04_PORT, &GPIO_InitStructure);
    GPIO_ResetBits(HC_SR04_PORT,HC_SR04_TR_PIN);
     
    GPIO_InitStructure.GPIO_Pin =   HC_SR04_EC_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
    GPIO_Init(HC_SR04_PORT, &GPIO_InitStructure);     
     
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); 
 
    TIM_DeInit(TIM2);
    TIM_TimeBaseStructure.TIM_Period = 65535; 
    TIM_TimeBaseStructure.TIM_Prescaler =(72-1); 
    TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);       

    TIM_Cmd(TIM2,DISABLE);     
}


static void OpenTimerForHc()  
{
        TIM_SetCounter(TIM2,0);
        TIM_Cmd(TIM2, ENABLE);
}
 
static void CloseTimerForHc()
{
        TIM_Cmd(TIM2, DISABLE);
}
 
u32 GetEchoTimer(void)
{
    u32 t = 0;
    t = TIM_GetCounter(TIM2);
    TIM2->CNT = 0; 
    delay_ms(50);
    return t;
}
 

float Hcsr04GetLength(void)
{
	HC_SR04_TR_SET;  
	delay_us(20);
	HC_SR04_TR_RESET;
	while(HC_SR04_EC_STA == 0); 
	OpenTimerForHc();
	while(HC_SR04_EC_STA == 1);
	CloseTimerForHc();   
	return 1.0*GetEchoTimer()*0.017; 
}



//BEEP_Init
void BEEP_Init(void)
{
 	GPIO_InitTypeDef GPIO_InitStructure;
 	RCC_APB2PeriphClockCmd(BEEP_CLK , ENABLE); 
	GPIO_InitStructure.GPIO_Pin = BEEP_PIN;
	#if 1
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
	#else
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; 
	#endif
	GPIO_Init(BEEP_PORT, &GPIO_InitStructure);
	BEEP_SET;
}

//BEEP_Test
void BEEP_Test(int cnt, int ms)
{
	int temp;
	for(temp=0;temp<cnt;temp++)
	{
		BEEP_RESET;
		delay_ms(ms);
		BEEP_SET;
		delay_ms(ms);
	}
}

//Door_Init
void Door_Init(void)
{
 	GPIO_InitTypeDef GPIO_InitStructure;
 	RCC_APB2PeriphClockCmd(DOOR_CLK , ENABLE); 
	GPIO_InitStructure.GPIO_Pin = DOOR_PIN;
	#if 1
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
	#else
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; 
	#endif
	GPIO_Init(DOOR_PORT, &GPIO_InitStructure);
	DOOR_SET;
}

//Door_Test
void Door_Test(void)
{
	uint8_t LEDSta = T_LED_STA;
	T_LED_SET;
	DOOR_RESET;
	delay_ms(8000);
	DOOR_SET;
	LEDSta?T_LED_SET:T_LED_RESET;
}


//T_LED_Init
void T_LED_Init(void)
{
 	GPIO_InitTypeDef GPIO_InitStructure;
 	RCC_APB2PeriphClockCmd(T_LED_CLK , ENABLE); 
	GPIO_InitStructure.GPIO_Pin = T_LED_PIN;
	#if 1
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
	#else
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; 
	#endif
	GPIO_Init(T_LED_PORT, &GPIO_InitStructure);
	T_LED_RESET;
}

//T_LED_Test
void T_LED_Test(void)
{
	int temp;
	for(temp=0;temp<6;temp++)
	{
		T_LED_SET;
		delay_ms(500);
		T_LED_RESET;
		delay_ms(500);
	}
}


//SR501_Init
void SR501_Init(void)
{
 	GPIO_InitTypeDef GPIO_InitStructure;
 	RCC_APB2PeriphClockCmd(SR501_CLK , ENABLE); 
	GPIO_InitStructure.GPIO_Pin = SR501_PIN;
	#if 0
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
	#else
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; 
	#endif
	GPIO_Init(SR501_PORT, &GPIO_InitStructure);
}

void SR501_Test(void)
{
	if(SR501_STA)
	{
		BEEP_RESET;
		delay_ms(1000);
		BEEP_SET;
	}
}


void RFID_GPIO_Init(void)
{
  GPIO_InitTypeDef  GPIO_InitStructure;
  
  /* Enable the GPIO Clock */
  RCC_APB2PeriphClockCmd(MF522_RST_CLK, ENABLE);

  /* Configure the GPIO pin */
  GPIO_InitStructure.GPIO_Pin = MF522_RST_PIN;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;

  GPIO_Init(MF522_RST_PORT, &GPIO_InitStructure);
	
  /* Enable the GPIO Clock */
  RCC_APB2PeriphClockCmd(MF522_MISO_CLK, ENABLE);

  /* Configure the GPIO pin */
  GPIO_InitStructure.GPIO_Pin = MF522_MISO_PIN;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;

  GPIO_Init(MF522_MISO_PORT, &GPIO_InitStructure);
	
  /* Enable the GPIO Clock */
  RCC_APB2PeriphClockCmd(MF522_MOSI_CLK, ENABLE);

  /* Configure the GPIO pin */
  GPIO_InitStructure.GPIO_Pin = MF522_MOSI_PIN;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;

  GPIO_Init(MF522_MOSI_PORT, &GPIO_InitStructure);
	
  /* Enable the GPIO Clock */
  RCC_APB2PeriphClockCmd(MF522_SCK_CLK, ENABLE);

  /* Configure the GPIO pin */
  GPIO_InitStructure.GPIO_Pin = MF522_SCK_PIN;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;

  GPIO_Init(MF522_SCK_PORT, &GPIO_InitStructure);
	
  /* Enable the GPIO Clock */
  RCC_APB2PeriphClockCmd(MF522_NSS_CLK, ENABLE);

  /* Configure the GPIO pin */
  GPIO_InitStructure.GPIO_Pin = MF522_NSS_PIN;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;

  GPIO_Init(MF522_NSS_PORT, &GPIO_InitStructure);
	
  /* Enable the GPIO Clock */
  RCC_APB2PeriphClockCmd(LED_CLK, ENABLE);

  /* Configure the GPIO pin */
  GPIO_InitStructure.GPIO_Pin = LED_PIN;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;

  GPIO_Init(LED_PORT, &GPIO_InitStructure);
}

void RFID_Init(void)
{
	RFID_GPIO_Init();
	LED_OFF;
	delay_10ms(10);
	PcdReset();
	PcdAntennaOff(); 
	PcdAntennaOn();  
	M500PcdConfigISOType( 'A' );
	LED_ON;
	delay_10ms(10);	
	LED_OFF;
	delay_10ms(10);
	LED_ON;
	delay_10ms(10);	
	LED_OFF;
}


void FOR_Init(void)
{
 	GPIO_InitTypeDef GPIO_InitStructure;
 	RCC_APB2PeriphClockCmd(FOR_CLK , ENABLE);  // 
	GPIO_InitStructure.GPIO_Pin = FOR_PIN; //
	#if 0
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
	#else
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;  //
	#endif
	GPIO_Init(FOR_PORT, &GPIO_InitStructure);
}

void FOR_Test(void)
{
	if(!FOR_STA)
	{
		BEEP_RESET;
		delay_ms(2000);
		BEEP_SET;
	}
}

void RCC_Configuration(void)
{
		ErrorStatus HSEStartUpStatus;
    RCC_DeInit();  //时钟管理重置  /* RCC system reset(for debug purpose) */
    RCC_HSEConfig(RCC_HSE_ON); //打开外部晶振  //Enable HSE
    HSEStartUpStatus = RCC_WaitForHSEStartUp();  //等待外部晶振就绪  //Wait till HSE is ready
    if(HSEStartUpStatus == SUCCESS)
    {
        RCC_HCLKConfig(RCC_SYSCLK_Div1);    //AHB使用系统时钟   //HCLK = SYSCLK
        RCC_PCLK2Config(RCC_HCLK_Div1);     //APB2为HCLK/1      //PCLK2 = HCLK/1
        RCC_PCLK1Config(RCC_HCLK_Div2);     //APB1为HCLK/2      //PCLK1 = HCLK/2
        RCC_ADCCLKConfig(RCC_PCLK2_Div4);   //设置ADC时钟频率
        //注：AHB主要负责外部存储器时钟。PB2负责AD，I/O，高级TIM，串口1。APB1负责DA，USB，SPI，I2C，CAN，串口2345，普通TIM。
        FLASH_SetLatency(FLASH_Latency_2);  //flash操作的延时   //Flash 2 wait state
        FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);   //flash读取缓冲，加速 //Enable Prefetch Buffer
     
        RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);  //PLLCLK = 8MHz * 9 = 72MHz
        RCC_PLLCmd(ENABLE);  //启动PLL //Enable PLL
        while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) //等待PLL启动  /* Wait till PLL is ready */
        {
        }
        RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //将PLL设置为系统时钟源  /* Select PLL as system clock source */
        while(RCC_GetSYSCLKSource() != 0x08)           //等待系统时钟源的启动  /* Wait till PLL is used as system clock source */
        {
        }
				SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);
    }
}
